package com.egao.common.module.world.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.Constants;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.world.entity.WorldGame;
import com.egao.common.module.world.mapper.WorldGameMapper;
import com.egao.common.module.world.service.WorldGameService;
import com.egao.common.module.world.vo.WorldGamePubliclyVO;
import com.egao.common.module.world.vo.WorldGameVO;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * WorldGame Service Impl
 *
 * @author root
 */
@Service
public class WorldGameServiceImpl extends ServiceImpl<WorldGameMapper, WorldGame> implements WorldGameService {

    @Override
    public List<WorldGame> listPage(PageParam<WorldGame> page) {
        return baseMapper.listPage(page);
    }

    @Override
    public JsonResult insertWorldGame(WorldGame worldGame, Integer loginUserId) {
        worldGame.setCreateBy(loginUserId);
        worldGame.setCreateTime(new Date());
        if (save(worldGame)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Override
    public JsonResult updateWorldGame(WorldGame worldGame) {
        if (updateById(worldGame)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult deleteWorldGame(WorldGame worldGame) {
        UpdateWrapper<WorldGame> uw = new UpdateWrapper<WorldGame>();
        uw.lambda().set(WorldGame::getDeleted, Constants.DELETED_FALSE)
                .eq(WorldGame::getId, worldGame.getId());
        if (update(uw)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public List<WorldGamePubliclyVO> listPubliclyPage(PageParam<WorldGamePubliclyVO> page) {
        return baseMapper.listPubliclyPage(page);
    }

    @Async
    @Override
    public void updateHits(WorldGame worldGame) {
        Integer hits = worldGame.getHits() != null ? worldGame.getHits() + 1 : 1;

        WorldGame update = new WorldGame();
        update.setId(worldGame.getId());
        update.setHits(hits);
        updateById(update);
    }
}
